<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/19
 * Time: 15:53
 */

namespace app\api\model;


class CouponCode extends BaseModel
{
    protected $autoWriteTimestamp = true;

    /**
     * 与coupon为一对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function coupon(){
        return $this->belongsTo('Coupon','c_id','id');
    }

    /**
     * 与workuser一对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function workuser(){
        return $this->belongsTo('WorkUser','source','id');
    }

    /**
     * 与user表与user多对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function user(){
        return $this->belongsTo('User','user_id','id')->field('id,avatar_url,nickname,create_time');
    }

    /**
     * 添加优惠卷
     * @param $data
     * @return array|false
     * @throws \Exception
     */
    public static function addCode($data){
        return (new CouponCode())->saveAll($data);
    }

    /**
     * 获取激活码列表
     * @param $c_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCodeList($c_id){
        return self::where('c_id','=',$c_id)
                ->where('user_id = 0')
                ->where('is_use = 0')
                ->select();
    }

    /**
     * 获取用户未用优惠卷
     * @param $user_id
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCodeListByUserId($user_id,$where){
        return self::where('user_id','=',$user_id)
            ->where($where)->field('c_id')->select();
    }

    /**
     * 获取用户优惠卷
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCouponByUserId($user_id){
        $data = self::where('user_id','=',$user_id)
            ->with(['coupon'])
            ->with(['workuser'])
            ->field('code,source,c_id,is_use,create_time,update_time')
            ->select();
        if (!$data->isEmpty()){
            foreach ($data as $value){
                $value->coupon->hidden(['quota','take_count','used_count','grounding_time','create_user','update_user','create_time','update_time','delete_time']);
                $value->workuser->visible(['id','name']);
            }
        }
        return $data;
    }
    /**
     * 更新激活码表
     * @param $code
     * @param $data
     * @return $this
     */
    public static function updateCode($code,$data){
        return self::where('code','=',$code)->update($data);
    }

    /**
     * 查询用户激活码
     * @param $user_id
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCouponCodeByUserAndWorkUser($user_id,$wu_id){
        $data = self::where('source','=',$wu_id)
                ->where('user_id','=',$user_id)->select();
        return $data;
    }

    /**
     * 获客详情
     * @param $c_id
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUsers($c_id,$wu_id){
        $data = self::where('source','=',$wu_id)
                ->where('c_id','=',$c_id)
                ->with(['user'])->select();
        foreach ($data as $item){
            $item->visible(['id','user_id','user']);
        }
        return $data;
    }
}